import { Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import { NavController, NavParams,AlertController,ModalController } from 'ionic-angular';
import { ChoosePointsPage } from '../choose-points/choose-points';
import { Mistake } from '../mistake';
import { MistakeService } from '../mistake.service';
import { SameExamplePage } from '../same-example/same-example';


@Component({
    selector: 'page-edit-mistakes',
    templateUrl: 'edit-mistakes.html',
    providers:[MistakeService],

  })
  export class EditMistakesPage implements OnInit{

      @Input("slides") slides: number[] = [];  
      @Input("pageNumber") pageNumber: number = 7;  
      @Output("slideClick") slideClick = new EventEmitter<number>();  
      
      selectedIndex: number = 0; 

      currentMistake:Mistake =  new Mistake("12345",null,"233","233","point119","233","233","233","233","233","","","","");
      currentPoint:string="point1";
      mistakeListByPoints: Mistake[];
      editFlag:boolean;
      currentCollect:boolean;
      collectMistakes: Mistake[];
      editStr:string = "edit";
      id:string;
      starnum = 3;
      flag:boolean;
      course:string = "语文";
      pointsSelected;
      source = " ";
      starList : string[];
      blingStarPath : string;
      darkStarPath : string;
      focus : number;
      collectFlags:boolean[];
      flagIndex:number = 0;
      public currentDate: String;
      public chosenDate: String;
     // public uploader:FileUploader = new FileUploader({url:URL});
      testCheckboxOpen: boolean;
      testCheckboxResult;

      mistake1: Mistake;

      constructor(public navCtrl: NavController,
                  public alertCtrl: AlertController,
                  public modalCtrl: ModalController,
                  public mistakeService: MistakeService,
                  public navparams:NavParams,
                 ){
        this.currentPoint = this.navparams.get("currentPoint");
        this.currentDate = this.getDate();
        this.chosenDate = this.currentDate;
        this.source = " ";
        this.collectMistakes = [];
        this.blingStarPath = "assets/icon/star.png";
        this.darkStarPath = "assets/icon/star-2.png";
        this.starList = [];
        this.pointsSelected = [];
        this.mistakeListByPoints = [];
        this.focus = 0;
        this.id = Date.now.toString();
        this.collectFlags = [];
        this.mistake1 = new Mistake("12345",null,"233","233","point119","233","233","233","233","233","233","","","");
        this.mistake1.misId = this.id;
        this.mistake1.points = "456"
       /* this.mistakeToSubmitList.forEach(
          element=> element.id = this.id,
          element=> element.mis_id = this.id);*/

        for(let i = 0;i < 5;i++){
          this.starList.push(this.darkStarPath);
        }

      }


      ngOnInit():void{
        this.currentPoint = this.navparams.get("currentPoint");
        this.getMistakesByPoint();
      }


      changeStatus(){
        console.log(this.editFlag);
        console.log(this.editStr);
        if(this.editFlag == false){
          this.editFlag = true;
          this.editStr = "complete";
        }else if(this.editFlag == true){
          this.editStr = "edit"
          this.editFlag = false;
          this.submitMistakes();
        }else{
          this.editFlag = true;
          this.editStr = "complete";
        }
      }

      changeCollectStatus(){
        console.log("collectFlags["+this.flagIndex+"] : "+this.collectFlags[this.flagIndex]);
        if(this.collectFlags[this.flagIndex] == false){
          this.collectFlags[this.flagIndex] = true;
          this.currentCollect = true;
          this.collectMistakes.push(this.currentMistake);
          console.log("add: this.collectMistake.length:"+this.collectMistakes.length);

        }else if(this.collectFlags[this.flagIndex] == true){
          this.collectFlags[this.flagIndex] = false;
          this.currentCollect = false;
          this.collectMistakes.splice(this.flagIndex,1);
          console.log("delete: this.collectMistake.length:"+ this.collectMistakes.length);

        }else{
          console.log("else: collectFlags["+this.flagIndex+"]: "+this.collectFlags[this.flagIndex]);
          this.collectFlags[this.flagIndex] = true;
          this.currentCollect = true;
          this.collectMistakes.push(this.currentMistake);

        }
      }

      //知识点
      presentModal() {
        if(this.editFlag == true){
          let modal = this.modalCtrl.create(ChoosePointsPage,{});
          modal.present();
          modal.onDidDismiss(pointsSelected => { this.pointsSelected = pointsSelected}
          );
          this.editFlag = true;
        }
      }

      //错误原因
      doMultiChoice(){
        if(this.editFlag == true){
        let alert = this.alertCtrl.create();
        alert.setTitle('错误原因');

        alert.addInput({
          type: 'checkbox',
          label: '计算错误',
          value: '计算错误',
          checked: true
        });

        alert.addInput({
            type: 'checkbox',
            label: '题目看错',
            value: '题目看错',
          });


        alert.addInput({
            type: 'checkbox',
            label: '掌握不牢固',
            value: '掌握不牢固',
          });

        alert.addButton('Cancel');
        alert.addButton({
          text: 'OK',
          handler: data => {
            this.testCheckboxOpen = false;
            this.testCheckboxResult = data;
            console.log(this.testCheckboxResult);
            this.testCheckboxResult.forEach(element => {
              console.log(element.value)
            });
          }
        });
        alert.present();
        this.editFlag = true;
      }
      }

      //掌握程度
      checkStar(i){
        if(this.editFlag == true){
        this.focus = i + 1;
        for(let j = 0;j < i + 1; j++){
          this.starList[j] = this.blingStarPath;
        }
        for(let j = i + 1;j<this.starList.length;j++){
          this.starList[j] = this.darkStarPath;
        }
      }
      }

      submitMistakes(){
         this.mistake1 = new Mistake(this.id,null,"133","133","point19",'2',"13","13","13","13","13","","","");
         this.mistake1.misId = this.id;
         this.mistake1.answPicUrl = "";
         this.mistake1.points = this.pointsSelected;
         this.mistake1.reason = this.testCheckboxResult;
         this.mistake1.masterDegree = this.focus;
         this.mistake1.editTime = this.currentDate+"";
        // this.mistake1 = new Mistake(this.id,"133","133","133","point19",'2',"13","13","13","13","13");
         console.log(this.mistake1.misId);
         this.mistakeService.submitMistakes(this.mistake1).then(data=>console.log("success"),
          error=>console.log(error));
      }

      getMistakesByPoint(){
        var url = "http://localhost:8080/mistake/getMistakesByPoint?currentPoint="+this.currentPoint;
        console.log(url);
        return this.mistakeService.getMistakesByPoints(url).then(mistakeList=>(
          this.mistakeListByPoints = mistakeList,
          this.currentMistake = mistakeList[0],
          this.collectFlags.push(false))
        )}
      gotoSameExample(){
        this.navCtrl.push(SameExamplePage,{

        })
      }

       onClick(index) {  
        this.flagIndex = index;
         if(this.collectFlags.length == 1){
           if(this.mistakeListByPoints.length > 1){
             for(var i = 1; i < this.mistakeListByPoints.length; i++){
                this.collectFlags.push(false);
             }
          }
         }
         this.selectedIndex = index;  
         this.currentMistake = this.mistakeListByPoints[index];
         this.currentCollect = this.collectFlags[index];
         this.slideClick.emit(index);  
     }  

     // 格式化日期，获取今天的日期
     getDate() {
      const Dates = new Date();
      const year: number = Dates.getFullYear();
      const month: any = ( Dates.getMonth() + 1 ) < 10 ? '0' + ( Dates.getMonth() + 1 ) : ( Dates.getMonth() + 1 );
      const day: any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
      return year + '-' + month + '-' + day;
  }


  }
